<?php
/**
 *  date: 2017/7/10  11:11
 *  Author: L先生
 *  encoding:utf-8  language:PHP
 */

namespace Admin\Model;


class AnswerModel extends AdminBaseModel
{
    protected $tableName = 'answer_formalinfo';

    /**
     *题库列表
     * @param $title
     * @return array
     * @Date 201707
     * @Author Lin
     */
    public function getQuestionList($title)
    {
        if($title)
            $map['title'] = array('like','%'.$title.'%');

        $qbModel = M('question_bank');
        $count =$qbModel->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page -> show();
        $result = $qbModel->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        return array($result,$show);
    }

    public function deleteAllQuestion($idArr)
    {
        foreach($idArr['id'] as $vo)
        {
            M('question_bank')->where(array('id'=>$vo))->delete();
        }
        return true;
    }

    /**
     *答题信息
     * @Date 201707
     * @Author Lin
     */
    public function getAnswerInfoList($username,$school,$area,$city)
    {
        $map = array();
        if($username){
            $map['mt_answer_formalinfo.user_id'] = M('users')->where(array('nickname'=>array('like','%'.$username.'%')))->getField('id');
        }
        if($school)
            $map['mt_answer_formalinfo.school'] = array('like','%'.$school.'%');
        if($area)
            $map['mt_answer_formalinfo.area'] = array('like','%'.$area.'%');
        if($city)
            $map['mt_answer_formalinfo.city'] =array('like','%'.$city.'%');

        //$order = array('score'=>'desc','total_time'=>'asc','id'=>'desc');
        $order = array('id'=>'desc','score'=>'desc','total_time'=>'asc',);

        $count = $this->where($map)->count();
        $Page = new \Think\Page($count,15);
        $show = $Page->show();

        $result = $this
            ->field('mt_answer_formalinfo.*,mt_users.nickname')
            ->where($map)
            ->join('LEFT JOIN mt_users  ON mt_users.id=mt_answer_formalinfo.user_id ')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($order)
            ->select();
        return array($result,$show);
    }

    /**
     *全选删除信息
     * @param $idArr
     * @return bool
     * @Date 201707
     * @Author Lin
     */
    public function deleteAllInfo($idArr)
    {
        foreach($idArr['id'] as $vo)
            $this->where(array('id'=>$vo))->delete();
        return true;
    }

}